package com.gcph.zhuoba.player.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.common.BaseActivity;
import com.gcph.zhuoba.player.client.PlayerPracticeClient;
import com.gcph.zhuoba.player.client.PlayerPreviewClient;
import com.gcph.zhuoba.util.L;

/**
 * 预览的activity
 * @author zhanghejie
 */
public class PlayerPreviewActivity extends BaseActivity {
	//退出
    private ImageView btn_preview_quit;
  
    private static Handler socketHandler;
    private PlayerPreviewClient socketClient;
    private String method;
    private String data;
    

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.fullScreen(true);
		setContentView(R.layout.activity_player_preview);
		Intent intent = this.getIntent(); 
		method=intent.getStringExtra("method");
		data=intent.getStringExtra("data");
		socketClient = PlayerPreviewClient.getInstance();
		
		socketHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
            	//配对失败
            	if(PlayerPracticeClient.PAIR_FAIL == msg.what)
            	{
            		Toast.makeText(PlayerPreviewActivity.this, "与终端配对失败", Toast.LENGTH_SHORT).show();
            		finish();
            	}
            	//配对成功
            	if(PlayerPracticeClient.PAIR_SUCC == msg.what)
            	{
            		//发送预览指令
            		socketClient.send("{\"method\":\""+method+"\",\"data\":\""+data+"\"}");
            	}
            }
		};
		
		
		//----------------------配对开始----------------------------
		new Thread(new Runnable() {
			@Override
			public void run() {
				String socket_ip = getResources().getString(R.string.socket_ip);
				int socket_port = Integer.parseInt(getResources().getString(R.string.socket_port));
				socketClient.setUpConnection(socket_ip,socket_port,socketHandler);
				L.d("======请求配对=====");
				socketClient.send("{\"method\":\"102\",\"deviceid\":\"1\",\"username\":\"test\"}");
				socketClient.handleReceive(socketHandler);
			}
		}).start();
		//----------------------配对结束----------------------------
		
		btn_preview_quit = (ImageView) this.findViewById(R.id.btn_preview_quit);
		//退出预览
		btn_preview_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
            	//退出断开连接
            	socketClient.exit();
            	finish();
            }
        });
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
}